package com.hexinfo.dmpro.dev.data.transfer.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.base.PageResult;
import com.hexinfo.dmpro.dev.data.transfer.dao.DesensitizeFieldMapMapper;
import com.hexinfo.dmpro.dev.data.transfer.model.DesensitizeFieldMap;
import com.hexinfo.dmpro.dev.data.transfer.model.DesensitizeFunctionConf;
import com.hexinfo.dmpro.dev.data.transfer.service.IDesensitizeFieldListService;
import com.hexinfo.dmpro.dev.data.transfer.service.IDesensitizeFieldMapService;
import com.hexinfo.dmpro.dev.data.transfer.service.IDesensitizeFunctionConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class DesensitizeFieldMapService extends ServiceImpl<DesensitizeFieldMapMapper, DesensitizeFieldMap> implements IDesensitizeFieldMapService {

    @Autowired
    private IDesensitizeFunctionConfService desensitizeFunctionConfService;

    @Autowired
    private IDesensitizeFieldListService desensitizeFieldListService;

    /**
     * @Method queryList
     * @Param
     * @param page
     * @param desensitizeFieldMap
     * @Return com.hexinfo.dmpro.common.base.PageResult
     * @Description 分页查询
     * @Author Wang zhihao
     * @Date 2024/1/5 11:19
     * @Version V1.0
     */
    @Override
    public PageResult queryList(Page page, DesensitizeFieldMap desensitizeFieldMap) {
        LambdaQueryWrapper<DesensitizeFieldMap> wrapper = beforeQuery(desensitizeFieldMap);
        Page returnPage = this.baseMapper.selectPage(page, wrapper);
        afterQuery(returnPage.getRecords());
        return new PageResult(returnPage.getRecords(), returnPage.getTotal());
    }

    /**
     * @Method queryListNoPage
     * @Param
     * @param desensitizeFieldMap
     * @Return java.util.List<com.hexinfo.dmpro.dev.data.transfer.model.DesensitizeFieldMap>
     * @Description 不分页查询
     * @Author Wang zhihao
     * @Date 2024/1/5 11:19
     * @Version V1.0
     */
    @Override
    public List<DesensitizeFieldMap> queryListNoPage(DesensitizeFieldMap desensitizeFieldMap) {
        LambdaQueryWrapper<DesensitizeFieldMap> wrapper = beforeQuery(desensitizeFieldMap);
        List<DesensitizeFieldMap> desensitizeFieldMapList = this.baseMapper.selectList(wrapper);
        afterQuery(desensitizeFieldMapList);
        return desensitizeFieldMapList;
    }

    private LambdaQueryWrapper<DesensitizeFieldMap> beforeQuery(DesensitizeFieldMap desensitizeFieldMap) {
        LambdaQueryWrapper<DesensitizeFieldMap> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(desensitizeFieldMap.getDesensitizeField())) {
            wrapper.like(DesensitizeFieldMap::getDesensitizeField, desensitizeFieldMap.getDesensitizeField());
        }
        if (StrUtil.isNotBlank(desensitizeFieldMap.getFunctionId())) {
            wrapper.eq(DesensitizeFieldMap::getFunctionId, desensitizeFieldMap.getFunctionId());
        }
        if (StrUtil.isNotBlank(desensitizeFieldMap.getDesensitizeField())) {
            wrapper.eq(DesensitizeFieldMap::getDesensitizeField, desensitizeFieldMap.getDesensitizeField());
        }
        return wrapper;
    }

    private void afterQuery(List<DesensitizeFieldMap> desensitizeFieldMapList) {
        for (DesensitizeFieldMap desensitizeFieldMap : desensitizeFieldMapList) {
            DesensitizeFunctionConf desensitizeFunctionConf = desensitizeFunctionConfService.getById(desensitizeFieldMap.getFunctionId());
            if (desensitizeFunctionConf != null) {
                desensitizeFieldMap.setFunctionName(desensitizeFunctionConf.getFunctionName());
                desensitizeFieldMap.setFunctionExplain(desensitizeFunctionConf.getFunctionExplain());
            }
        }
    }

    /**
     * @Method removeByIds
     * @Param
     * @param ids
     * @Return boolean
     * @Description 批量删除
     * @Author Wang zhihao
     * @Date 2024/1/4 17:07
     * @Version V1.0
     */
    @Override
    public boolean removeByIds(List<String> ids) {
        desensitizeFieldListService.removeByFunctionMapIds(ids);
        ids.forEach(this::removeById);
        return true;
    }

    /**
     * @Method removeByFunctionIds
     * @Param
     * @param functionIds
     * @Return boolean
     * @Description 根据脱敏函数ID删除脱敏字段配置表
     * @Author Wang zhihao
     * @Date 2024/1/4 17:08
     * @Version V1.0
     */
    @Override
    @Transactional
    public boolean removeByFunctionIds(List<String> functionIds) {
        // 根据functionId获取所有符合的映射
        List<DesensitizeFieldMap> desensitizeFieldMapList = queryListByFunctionIds(functionIds);
        if (CollectionUtil.isNotEmpty(desensitizeFieldMapList)) {
            List<String> ids = desensitizeFieldMapList.stream().map(DesensitizeFieldMap::getId).collect(Collectors.toList());
            removeByIds(ids);
        }
        return true;
    }

    /**
     * @Method queryListByFunctionIds
     * @Param
     * @param functionIds
     * @Return java.util.List<com.hexinfo.dmpro.dev.data.transfer.model.DesensitizeFieldMap>
     * @Description 根据functionId获取所有的映射信息
     * @Author Wang zhihao
     * @Date 2024/1/4 18:17
     * @Version V1.0
     */
    private List<DesensitizeFieldMap> queryListByFunctionIds(List<String> functionIds) {
        LambdaQueryWrapper<DesensitizeFieldMap> wrapper = Wrappers.lambdaQuery();
        wrapper.in(DesensitizeFieldMap::getFunctionId, functionIds);
        return this.baseMapper.selectList(wrapper);
    }

}
